package com.translate.web.service.impl;

import com.translate.web.config.contract.IOSSConfig;
import com.translate.web.constant.RedisConstant;
import com.translate.web.dao.ProductImgMapper;
import com.translate.entity.ProductImg;
import com.param.contract.IIdParam;
import com.translate.web.param.contract.IProductImgAddParam;
import com.translate.web.service.IProductImgService;
import com.basic.service.impl.BaseServiceImpl;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

@Service
public class ProductImgServiceImpl extends BaseServiceImpl<ProductImg> implements IProductImgService {
	
	private RedisTemplate<String, List<ProductImg>> redisTemplate;

	
	private IOSSConfig config;
	
	@Autowired
	public void setConfig(IOSSConfig config) {
		this.config = config;
	}

	@Autowired
	public void setRedisTemplate(RedisTemplate<String, List<ProductImg>> redisTemplate) {
		this.redisTemplate = redisTemplate;
	}

	public ProductImgServiceImpl(ProductImgMapper mapper) {
		super(mapper);
	}

	protected Class<ProductImg> getEntityClass() {
		return ProductImg.class;
	}

	@Override
	public <S extends IProductImgAddParam> int parseBatchInsert(List<S> accessLabelParams, int assocId) {
		
		if (null == accessLabelParams) {
			return 1;
		}

		for (S s : accessLabelParams) {
			s.setProductId(assocId);
		}

		return this.batchInsert(this.paramToT(accessLabelParams, ProductImg.class));
	}
	
	
	/**
	 * 比较更新主要条件
	 * @return
	 */
	protected <S> boolean compareMasterCondition(S condition, ProductImg data) {

		IProductImgAddParam productImgAddParam = (IProductImgAddParam)condition;
		
		String img = productImgAddParam.getImg();
		
		return  img.equals(data.getImg());
	}
	

	protected ProductImg getEntityInstance() {
		return new ProductImg();
	}
	
	protected ProductImg[] getUpdateData(int size) {
		return new ProductImg[size];
	}
	
	/**
	 * 
	 * @param <S>
	 * @param entity
	 * @param obtainedFromTheMap
	 * @param assocId            关联id 由子类指定
	 * @param userId             不需要则填0
	 */
	protected <S> void toEntity(ProductImg entity, S obtainedFromTheMap, int assocId, int userId) {

		IProductImgAddParam customerPoolAddMemberParam = (IProductImgAddParam) obtainedFromTheMap;

		entity.setProductId(assocId);

		Integer id = null;
		
		if (obtainedFromTheMap instanceof IIdParam) {
			id = ((IIdParam) obtainedFromTheMap).getId();
			
			if (null != id) {
				entity.setId(id);
			}
		}

		entity.setImg(customerPoolAddMemberParam.getImg().replaceAll(config.getLocalPicURL(), ""));
	}

	@Override
	public List<ProductImg> getProductImgs(IIdParam param) {
		List<ProductImg> productImgs = this.getProductImgsToCache(param);
		
		String url = this.config.getLocalPicURL();
		
		for (ProductImg productImg : productImgs) {
			
			productImg.setImg(url + productImg.getImg());
		}
		
		return productImgs;
	}
	
	protected List<ProductImg> getProductImgsToCache(IIdParam param) {
		int id = param.getId();
		String key = RedisConstant.getImageKey(id);
		
		ValueOperations<String, List<ProductImg>> valueOperations = this.redisTemplate.opsForValue();
		
		List<ProductImg> order = valueOperations.get(key);
		
		if (null != order) {
			return order;
		}
		
		ProductImgMapper productImgMapper = this.getBaseMapper();
		
		order = productImgMapper.getProductImgs(id);
		
		if (null == order) {
			order = new ArrayList<ProductImg>(0);
			
			valueOperations.set(key, order, 3, TimeUnit.SECONDS);
			
			return order;
		}
		
		valueOperations.set(key, order, 17, TimeUnit.SECONDS);

		return order;
	}
}